/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fasea.model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lars-wolfram
 */
public class IndexingFolderList implements Iterable<IndexingFolder> {

    private List<IndexingFolder> indexingFolderEntrys = new ArrayList<IndexingFolder>();

    public IndexingFolderList() {
    }

    IndexingFolderList(String indexingFolders, String fileRestrictions) {
        fileRestrictions = removeOuterBrackets(fileRestrictions);
        String[] indexingFolderPaths = indexingFolders.split(File.pathSeparator);
        String[] allFileRestrictions = fileRestrictions.split(File.pathSeparator);
        for (int i = 0; i < indexingFolderPaths.length && !indexingFolderPaths[0].isEmpty(); ++i) {
            String[] fileRestrictionsForFolder = removeOuterBrackets(allFileRestrictions[i]).split(",");
            IndexingFolder indexingFolder = new IndexingFolder(new File(indexingFolderPaths[i]), fileRestrictionsForFolder);
            indexingFolderEntrys.add(indexingFolder);
        }
    }

    private String removeOuterBrackets(String indexingFolderFileRestrictions) {
        if (indexingFolderFileRestrictions.startsWith("[") && indexingFolderFileRestrictions.lastIndexOf("]") == indexingFolderFileRestrictions.length()-1) {
            return indexingFolderFileRestrictions.substring(1, indexingFolderFileRestrictions.length() - 1);
        }
        return indexingFolderFileRestrictions;
    }

    public void removeAll() {
        indexingFolderEntrys.clear();
    }

    public void add(IndexingFolder indexingFolder) {
        indexingFolderEntrys.add(indexingFolder);
    }

    String getIndexingFoldersString() {
        StringBuilder b = new StringBuilder();
        for (IndexingFolder folder : indexingFolderEntrys) {
            try {
                if (b.length() != 0) {
                    b.append(File.pathSeparator);
                }
                b.append(folder.getIndexingFolder().getCanonicalPath());
            } catch (IOException ex) {
                Logger.getLogger(IndexProperties.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return b.toString();
    }

    String getIndexingFolderFileRestrictionsString() {
        StringBuilder b = new StringBuilder();
        b.append("[");
        for (IndexingFolder folder : indexingFolderEntrys) {
            if (b.length() != 1) {
                b.append(File.pathSeparator);
            }
            b.append("[");
            for (String restriction : folder.getFileExtensionRestrictions()) {
                if (b.length() != 1) {
                    b.append(",");
                }
                b.append(restriction);
            }
            b.append("]");
        }
        b.append("]");
        return b.toString();
    }

    public int size() {
        return indexingFolderEntrys.size();
    }

    public IndexingFolder get(int row) {
        return indexingFolderEntrys.get(row);
    }

    public Iterator<IndexingFolder> iterator() {
        return indexingFolderEntrys.iterator();
    }
}
